package com.mobile.check.dialogs;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;

import java.util.List;

import cn.droidlover.xdroidmvp.base.BaseRecyclerAdapter;
import cn.droidlover.xdroidmvp.base.ListViewItemClickListener;
import cn.droidlover.xdroidmvp.base.RecyclerViewHolder;
import com.mobile.check.app.R;


/**
 * [底部弹出dialog]
 *
 * @author devin.hu
 * @version 1.0
 * @date 2014-1-15
 **/
public class BottomListDialog extends Dialog {

    RecyclerView mRecyclerview;
    BaseRecyclerAdapter<BottomItem> mBaseRecyclerAdapter;
    BottomListSelectListener listSelectListener;

    /**
     * @param context
     */
    public BottomListDialog(Context context, BottomListSelectListener listSelectListener) {
        super(context, R.style.dialogFullscreen);
        this.listSelectListener = listSelectListener;
    }

    /**
     * @param context
     */
    public BottomListDialog(int type, Context context) {
        super(context, R.style.dialogFullscreen);
    }

    /**
     * @param context
     * @param theme
     */
    public BottomListDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_bottomlist);
        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        layoutParams.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
        layoutParams.dimAmount = 0.5f;
        window.setGravity(Gravity.BOTTOM);
        window.setAttributes(layoutParams);
        window.setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        initRecyclerView();
    }


    public void initRecyclerView() {
        mRecyclerview = findViewById(R.id.recyclerview);
        final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        mRecyclerview.setLayoutManager(layoutManager);
        mBaseRecyclerAdapter = new BaseRecyclerAdapter<BottomItem>(getContext()) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.item_dialog_bottomlist;
            }

            @Override
            public void bindData(RecyclerViewHolder holder, int position) {
                BottomItem item = getItemByPosition(position);
                holder.setText(R.id.itemNameTv, item.getName());
            }
        };
        mRecyclerview.setAdapter(mBaseRecyclerAdapter);
        mBaseRecyclerAdapter.setItemClickListener(new ListViewItemClickListener() {
            @Override
            public void itemClick(int position) {
                BottomItem itemName = mBaseRecyclerAdapter.getItemByPosition(position);
                listSelectListener.itemClickCallback(itemName.getT());
                dismiss();
            }


        });
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        dismiss();
        return true;
    }


    public void disPlay(List<BottomItem> itemList) {
        show();
        mBaseRecyclerAdapter.addList(true, itemList);
    }


    public interface BottomListSelectListener<T> {
        void itemClickCallback(T t);
    }



}

